package com.ming.mina.client;

import com.ming.mina.handel.TestHandel;
import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.transport.socket.nio.NioDatagramConnector;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

/**
 * @Auther: tangC
 * @Date: 2020/7/30 13:44
 * @Description:
 */

public class TestClient {

    public static void main(String[] args) {
        NioDatagramConnector connector = new NioDatagramConnector();
        connector.getFilterChain().addLast("codec",
                new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
        connector.setHandler(new TestHandel());
        ConnectFuture future = connector.connect(new InetSocketAddress("localhost", 9999));
       System.out.println(future.isConnected());
       System.out.println(future.getException());
        while (!future.isConnected()){
            try {
                Thread.sleep(100);
                System.out.println(future.isConnected());
            }catch (Exception e){
            }
        }
        IoSession session = future.getSession();
        session.write("15");
//        while (!future.isConnected()){
//            try {
//                Thread.sleep(100);
//            }catch (Exception e){
//
//            }
//            if (future.isConnected()){
//                break;
//            }
//        }
//        IoSession session = future.getSession();
//        session.write("1111");
    }
}
